package com.rwind.dynamic.controller;

import com.rwind.dynamic.common.MessageDto;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.Map;

public class HealthController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) {
        return new ModelAndView(new MappingJacksonSingleView(), "health", MessageDto.getSuccess(null));
    }


    public class MappingJacksonSingleView extends MappingJackson2JsonView {

        @Override
        @SuppressWarnings("unchecked")
        protected Object filterModel(Map<String, Object> model) {
            Object result = super.filterModel(model);
            if (!(result instanceof Map)) {
                return result;
            }

            Map map = (Map) result;
            if (map.size() == 1) {
                return map.values().toArray()[0];
            }
            return map;
        }

    }

}
